<?php
namespace catchAdmin\storehouse\model;

use catchAdmin\product\model\Pack;
use catchAdmin\stock\model\Pick;
use catchAdmin\storehouse\model\search\WarehouseOutSonSearch;
use catcher\base\CatchModel;
use think\facade\Db;

class WarehouseOutSon extends CatchModel
{

    use WarehouseOutSonSearch;
    
    protected $name = 'db_warehouse_out_son';
    protected $field = [
            'id', //
			'warehouse_out_id', // 出库主单编号
			'warehouse_out_son_id', // 出库明细编号
			'goods_id', // 商品编号
			'HSCODE', // HSCODE
			'goods_name', // 商品名称
			'goods_Cname', // 商品中文名称
			'goods_Ename', // 商品英文名称
			'supplier_id', // 供应商编号
			'supplier_name', // 供应商名称
			'manufacturer_id', // 制造商编号
			'manufacturer_name', // 制造商名称
			'packing_id', // 包装编号
			'p_barcode', // 包装条形码
			'packing_num', // 包装数量
			'unit_name', // 包装单位级别名称（字典名称）
			'number', // 要求出库数量
			'box_number', // 商品箱数
			'tray_number', // 托盘数
			'pick_number', // 拣货数量
			'stock_number', // 仓库数量
			'status', // 数据状态0
			'out_number', // 出库数量
            'conditions_warehousing_no',//出库条件，根据入库单号筛选
			'tenant_id', // 商户id
            'creator_id', // 创建者ID
			'created_at', // 创建时间
			'updated_at', // 更新时间
			'deleted_at', // 删除状态，0未删除 >0 已删除
            'PO',
            'DO',
            'SN',
            'CO',
            'LOT1',
            'LOT2',
            'LOT3',
            'new_item_number',//项号
            'edi_json',
            'if_back_sn',//是否回传SN
            'back_sn',//SN序列号
    ];
    /**
     * 用户列表
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['wos.tenant_id','=',$tenant_id];
        }
        return $this
            ->alias('wos')
            ->catchSearch()
            ->leftJoin('db_provider sup','sup.id = wos.supplier_id ')
            ->leftJoin('db_provider man','man.id = wos.manufacturer_id ')
            ->where($where)
            ->field('wos.*,sup.account_name as _supplier_name,man.account_name as _manufacturer_name')
//            ->withoutField('tenant_id,creator_id,updated_at,deleted_at')
//            ->order($this->aliasField('id'), 'desc')
            ->order($this->aliasField('id'), 'asc')
            ->paginate()
            ->each(function (&$item){

                $product = Db::table('db_product')
                    ->field('new_carton_size,serial_number,item_number_system,sku,packing_id,describe,goods_Cname,goods_Ename,HSCODE,new_item_number')
                    ->where('goods_id',$item['goods_id'])->find();
                $item['describe'] = $product['describe'];
                $item['sku'] = $product['sku'];
                $item['goods_Cname'] = $product['goods_Cname'];
                $item['goods_Ename'] = $product['goods_Ename'];
                $item['serial_number'] = $product['serial_number'];
                $item['item_number_system'] = $product['item_number_system'];
                $item['HSCODE'] = $product['HSCODE'];
                $item['new_item_number'] = $product['new_item_number'];
                $item['new_carton_size'] = $product['new_carton_size'];
                $packing_id = explode(',',$product['packing_id']);
                $where = [];
                $where[] = ['packing_id','in',$packing_id];
                $list = Db::table('db_packing')->field('packing_id,unit_name,packing_num,layup_num')->where($where)->select()->toArray();
                foreach ($list as $k=>$v){
                    $list[$k]['value'] = $v['packing_id'];
                    $list[$k]['label'] = '1/'.$v['layup_num'].'/'.$v['packing_num'].'(托/箱/件)';
                    //$list[$k]['label'] = '包装数量('.$v['packing_num'].')--包装单位('.$v['unit_name'].')--托盘可放置' . $v['layup_num']  . $v['unit_name'];
                }
                $item['packing_list'] = $list;

                $item['warehousing_select_no'] = !empty($item['conditions_warehousing_no'])
                    ? explode(',',$item['conditions_warehousing_no'])
                    : [];

                $packing_info = (new Pack)->where('packing_id',$item['packing_id'])->find()->toArray();
                $item['_packing_hierarchy'] = "托/" . $packing_info['unit_name'] . "/件";
                $item['_packing_num'] = "1/" . $packing_info['layup_num'] . "/" . $packing_info['layup_num'] * $packing_info['packing_num'];
                $item['_packing_min_unit'] = $packing_info['packing_num'];
                $item['packing_info'] = $packing_info;
                $item['sn_list'] = Db::table('db_pick')
                    ->where([
                        'warehouse_out_id' => $item['warehouse_out_id'],
                        'warehouse_out_son_id' => $item['warehouse_out_son_id'],
                        'deleted_at'=>0
                    ])->column('sn');
                $item['sn_list'] = !empty($item['sn_list']) ? implode(',', $item['sn_list']) : '';

            });
    }

    /**
     *  无分页列表
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getAttrList($tenant_id=0)
    {
       $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        return $this
            ->catchSearch()
            ->field('id,goods_id,goods_name,barcode,packing_id')
            ->where($where)
            ->order($this->aliasField('id'), 'desc')
            ->select();
    }
}
